package leetcode;

public class a112路径总和 {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root==null){
            return false;
        }
        return haspath(root,targetSum-root.val);

    }

    public boolean haspath(TreeNode root,int sum) {
        if (root.left==null&&root.right==null&&sum==0){
            return true;
        }
        if (root.left==null&&root.right==null&&sum!=0){
            return false;
        }
        if (root.left!=null){
            sum-=root.left.val;
            if (haspath(root.left,sum)) return true;
            sum+=root.left.val;
        }
        if (root.right!=null){
            sum-=root.right.val;
            if (haspath(root.right,sum)) return true;
            sum+=root.right.val;
        }

        return false;

    }
}
